home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / bitmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  5.6 KB  |  197 lines

  1. /* $Id: bitmap.c,v 1.3 1996/11/06 04:23:18 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.0
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: bitmap.c,v $
  26.  * Revision 1.3  1996/11/06 04:23:18  brianp
  27.  * replaced 0 with GL_COLOR_INDEX in gl_unpack_bitmap()
  28.  *
  29.  * Revision 1.2  1996/09/15 14:18:10  brianp
  30.  * now use GLframebuffer and GLvisual
  31.  *
  32.  * Revision 1.1  1996/09/13 01:38:16  brianp
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37.  
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "bitmap.h"
  41. #include "context.h"
  42. #include "feedback.h"
  43. #include "image.h"
  44. #include "macros.h"
  45. #include "pb.h"
  46. #include "types.h"
  47.  
  48.  
  49.  
  50.  
  51. /*
  52.  * Unpack a bitmap image
  53.  */
  54. struct gl_image *gl_unpack_bitmap( GLcontext* ctx,
  55.                                    GLsizei width, GLsizei height,
  56.                                    const GLubyte *bitmap )
  57. {
  58.    return gl_unpack_image( ctx, width, height,
  59.                            GL_COLOR_INDEX, GL_BITMAP, GL_BITMAP,
  60.                            bitmap, GL_FALSE );
  61. }
  62.  
  63.  
  64.  
  65.  
  66. /*
  67.  * Do actual rendering of a bitmap.
  68.  */
  69. void gl_render_bitmap( GLcontext* ctx,
  70.                        GLsizei width, GLsizei height,
  71.                        GLfloat xorig, GLfloat yorig,
  72.                        GLfloat xmove, GLfloat ymove,
  73.                        const struct gl_image *bitmap )
  74. {
  75.    struct pixel_buffer *PB = ctx->PB;
  76.    GLint bx, by;      /* bitmap position */
  77.    GLint px, py, pz;  /* pixel position */
  78.    GLubyte *ptr;
  79.  
  80.    if (ctx->NewState) {
  81.       gl_update_state(ctx);
  82.       PB_INIT( PB, GL_BITMAP );
  83.    }
  84.  
  85.    if (ctx->Visual->RGBAflag) {
  86.       GLint r, g, b, a;
  87.       r = (GLint) (ctx->Current.RasterColor[0] * ctx->Visual->RedScale);
  88.       g = (GLint) (ctx->Current.RasterColor[1] * ctx->Visual->GreenScale);
  89.       b = (GLint) (ctx->Current.RasterColor[2] * ctx->Visual->BlueScale);
  90.       a = (GLint) (ctx->Current.RasterColor[3] * ctx->Visual->AlphaScale);
  91.       PB_SET_COLOR( ctx, PB, r, g, b, a );
  92.    }
  93.    else {
  94.       PB_SET_INDEX( ctx, PB, ctx->Current.RasterIndex );
  95.    }
  96.  
  97.    px = (GLint) ( (ctx->Current.RasterPos[0] - xorig) + 0.0F );
  98.    py = (GLint) ( (ctx->Current.RasterPos[1] - yorig) + 0.0F );
  99.    pz = (GLint) ( ctx->Current.RasterPos[2] * DEPTH_SCALE );
  100.    ptr = (GLubyte *) bitmap->Data;
  101.  
  102.    for (by=0;by<height;by++) {
  103.       GLubyte bitmask;
  104.  
  105.       /* do a row */
  106.       bitmask = 128;
  107.       for (bx=0;bx<width;bx++) {
  108.          if (*ptr&bitmask) {
  109.             PB_WRITE_PIXEL( PB, px+bx, py+by, pz );
  110.          }
  111.          bitmask = bitmask >> 1;
  112.          if (bitmask==0) {
  113.             ptr++;
  114.             bitmask = 128;
  115.          }
  116.       }
  117.  
  118.       PB_CHECK_FLUSH( ctx, PB )
  119.  
  120.       /* get ready for next row */
  121.       if (bitmask!=128)  ptr++;
  122.    }
  123.  
  124.    gl_flush_pb(ctx);
  125. }
  126.  
  127.  
  128.  
  129.  
  130. /*
  131.  * Execute a glBitmap command:
  132.  *   1. check for errors
  133.  *   2. feedback/render/select
  134.  *   3. advance raster position
  135.  */
  136. void gl_Bitmap( GLcontext* ctx,
  137.                 GLsizei width, GLsizei height,
  138.             GLfloat xorig, GLfloat yorig,
  139.             GLfloat xmove, GLfloat ymove,
  140.                 const struct gl_image *bitmap )
  141. {
  142.    if (width<0 || height<0) {
  143.       gl_error( ctx, GL_INVALID_VALUE, "glBitmap" );
  144.       return;
  145.    }
  146.    if (INSIDE_BEGIN_END(ctx)) {
  147.       gl_error( ctx, GL_INVALID_OPERATION, "glBitmap" );
  148.       return;
  149.    }
  150.    if (ctx->Current.RasterPosValid==GL_FALSE) {
  151.       /* do nothing */
  152.       return;
  153.    }
  154.  
  155.    if (ctx->RenderMode==GL_RENDER) {
  156.       GLboolean completed = GL_FALSE;
  157.       if (ctx->Driver.Bitmap) {
  158.          /* let device driver try to render the bitmap */
  159.          completed = (*ctx->Driver.Bitmap)( ctx, width, height, xorig, yorig,
  160.                                             xmove, ymove, bitmap );
  161.       }
  162.       if (!completed) {
  163.          /* use generic function */
  164.          gl_render_bitmap( ctx, width, height, xorig, yorig,
  165.                            xmove, ymove, bitmap );
  166.       }
  167.    }
  168.    else if (ctx->RenderMode==GL_FEEDBACK) {
  169.       GLfloat color[4], texcoord[4], invq;
  170.       color[0] = ctx->Current.IntColor[0] * ctx->Visual->InvRedScale;
  171.       color[1] = ctx->Current.IntColor[1] * ctx->Visual->InvGreenScale;
  172.       color[2] = ctx->Current.IntColor[2] * ctx->Visual->InvBlueScale;
  173.       color[3] = ctx->Current.IntColor[3] * ctx->Visual->InvAlphaScale;
  174.       invq = 1.0F / ctx->Current.TexCoord[3];
  175.       texcoord[0] = ctx->Current.TexCoord[0] * invq;
  176.       texcoord[1] = ctx->Current.TexCoord[1] * invq;
  177.       texcoord[2] = ctx->Current.TexCoord[2] * invq;
  178.       texcoord[3] = ctx->Current.TexCoord[3];
  179.       FEEDBACK_TOKEN( ctx, (GLfloat) GL_BITMAP_TOKEN );
  180.       /* TODO: Verify XYZW values are correct: */
  181.       gl_feedback_vertex( ctx, ctx->Current.RasterPos[0] - xorig,
  182.               ctx->Current.RasterPos[1] - yorig,
  183.               ctx->Current.RasterPos[2],
  184.               ctx->Current.RasterPos[3],
  185.               color, ctx->Current.Index, texcoord );
  186.    }
  187.    else if (ctx->RenderMode==GL_SELECT) {
  188.       /* Bitmaps don't generate selection hits.  See appendix B of 1.1 spec. */
  189.    }
  190.  
  191.    /* update raster position */
  192.    ctx->Current.RasterPos[0] += xmove;
  193.    ctx->Current.RasterPos[1] += ymove;
  194. }
  195.  
  196.  
  197.